package com.example.action_communication;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {
	private Button type1;
	private Button type2;
	private static EditText editText1;
	private static EditText editText2;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initView();
		initHandler();
	}

	private void initHandler() {
		// TODO Auto-generated method stub
		
		type1.setOnClickListener(new View.OnClickListener() {
		
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Log.i("Action_Communi","Type 1");
				//使用Intent 進行不同頁面的呼叫
				Intent it = new Intent();
				//setClass(from where , to where); 
				//因為現在位於匿名類別內，所以欲找到自己的位置須為 MainActivity.this 
				it.setClass(MainActivity.this , Activity2.class);
				//使用Bundle 進行不同頁面間的資料傳遞。
				Bundle b = new Bundle();
				b.putString("Name", editText1.getText()+"");
				b.putInt("Age",Integer.parseInt(editText2.getText()+""));
				it.putExtras(b);
				//startActivity() 後接 finish() 為只去不回的方式  ex: app載入前的開場
				//搭配timer 可做出想要的效果
				startActivity(it);
				finish();
				
			}
		});
		
		type2.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Log.i("Action_Communi","Type 2");
				Intent it = new Intent();
				it.setClass(MainActivity.this , Activity3.class);
				startActivityForResult(it,0);
			}
		});
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if(requestCode == 0 ){
			String message = data.getExtras().getString("Result");
			Log.i("Action_Communi","Message : "+ message);
			Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
		}
	}

	private void initView() {
		// TODO Auto-generated method stub
		type1 = (Button) this.findViewById(R.id.button1);
		type2 = (Button) this.findViewById(R.id.button2);
		editText1 = (EditText) this.findViewById(R.id.editText1);
		editText2 = (EditText) this.findViewById(R.id.editText2);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
